#Jupyter magic command to improve teh display of charts in the Notebook
%matplotlib inline

4. Report writing and scenario results#

ModelFlow, standard pandas routines and other python libraries like Matplotlib and Plotly can be used to visualize and compare dataframes and therefore the results from scenarios – as indeed has been done in the preceding paragraphs.

In addition, ModelFlow also provides several specific routines that make such comparisons easier.

4.1. Prepare python session#

from modelclass import model 
import modelmf       # Add useful features to pandas dataframes 
                     # using utlities initially developed for modelflow

model.widescreen()   # These modelflow commands ensure that outputs from modelflow play well with Jupyter Notebook
model.scroll_off()

4.1.1. Load model object and simulation results from Scenario Analysis Chapter#

mpak,_ = model.modelload(r'..\models\mpakwScenarios.pcim', run=False)
mpak.smpl(2020,2040);
file read:  C:\modelflow manual\papers\mfbook\content\models\mpakwScenarios.pcim

4.2. The Keep option#

The Keep option facilitates the comparison of results from different scenarios run on a give model object. In each of the simulations executed above, the keep option was activated. This causes the results from each simulation to be stored in the model object in a DataFrame that can be identified by the descriptor given to it.

The Keep option facilitates the comparison of results from different scenarios run on a give model object. In each of the simulations executed above, the keep option was activated. This causes the results from each simulation to be stored in the model object in a DataFrame that can be identified by the descriptor given to it.

By default all variables are kept, if the user wishes to restrict the kept variables to a sub-set, they can do so by setting the keep_variables when the simulation is run, where

For example:

mpak(CTempExogAllKG,2020,2040,keep='2.5% increase in C 2025-27 -- exog whole period --KG=True',keep_variables='PAKNYGDPMKTPKN PAKNE*KN PAKEM*')

would run a simulation between 2020 AND 2040 CALLED 2.5% increase in C 2025-27 – exog whole period –KG=True but keep only the variables that matched the search string in keep_variables.

4.3. The .keep_plot() method#

The keep_plot method can be used to plot and compare results from the various scenarios that had been run earlier using the keep= option.

By default the results across all scenarios for each selected variables will be shown on one chart at a time. NB: the 2020,2040 restricts output to the period 2020 2040 the period over which the shocks were run. Without that the graphs would display for the whole time period defined in the model object mpak.

mpak.keep_plot('PAKNYGDPMKTPCN PAKNECONPRVTKN PAKNEIMPGNFSKN',2020,2040,  legend=True);
#show for each variable on a separate chart the results from each kept scenario
../../_images/b14b98ab4b8301336bfa807c9c13f23b0408dc9fac76f5f4362a65eaddcf47a0.png ../../_images/40a1d5bf799485feedcefe6389594bc154d5abf8bf1fb40cb924fa02f9ef177b.png ../../_images/a4b1c08de7fa743b252e72e939c94bb3ab14a97f7bd6488b75d26fb4423174fe.png

4.3.1. keep_plot() options#

The variables to be displayed are listed as first argument. Variable names can include wildcards (using * for any string and ? for any character).

Time period to be displayed start=2020 end=2040

Restrict the period for which data are displayed. In the above example the option names (start, end) are not specified because the options appear in correct order defined by the keep_plot function. In such cases,python assumes that they apply to start and end.

Transformation of data displayed:

showtype=

Use this operator

‘level’ (default)

No transformation

‘growth’

The growth rate in percent

‘change’

The yearly change (\(\Delta\))

legend placement

legend=

Use this operator

False (default)

The legends are placed at the end of the corresponding line

True

The legends are places in a legend box

Often it is useful to compare the scenario results with the baseline result. This is done with the diff argument.

diff=

Use this operator

False (default)

All entries in the keep_solution dictionary are displayed

True

The difference to the first entry is shown.

It can also be useful to compare the scenario results with the baseline result measured in percent. This is done with the diffpct argument.

diffpct=

Use this operator

False (default)

All entries in the keep_solution dictionary is displayed

True

The difference in percent to the first entry is shown

Note

'keep_plot() and .keep_plot_multi() return a python object that points to the in memory version of the rendered figure(s). This object can be used to modify the graph (see examples towards the end of this chapter).

savefig='[path/]<prefix>.<extension>' Will create a number of files with the charts.
The files will be saved location with name <path>/<prefix><variable name>.<extension>
The extension determines the format of the saved file: pdf, svg and png are the most common extensions.

4.3.2. An example using the diff=True option#

When diff=True (or 1) results will be shown all of the selected scenarios presented as the level change in selected variables with respect to the first scenario – in this instance the scenario saved with the name baseline.

Warning

Note in this instance baseline and basedf are the same because they were defined that way. However, there is nothing in the system that guarantees that the first keep scenario will be the baseline or the basedf scenario.

#Set the default period for the mpak object to 2020 2040
# because of this future calls to keep_plot or anyother model objkect method
# will operate over the period 2020 2040 only.
mpak.smpl(2020,2040) 
mpak.keep_plot('PAKNYGDPMKTPCN PAKNECONPRVTKN PAKNEIMPGNFSKN', diff=1, legend=True);
../../_images/996aac57095cd9576f15f69868e3db68e8fe46fcb1f1ab52f32def7dc24eaf40.png ../../_images/3981a445609c593ab200e2a2291ca40272f28e32d275cabbbd23d330af2ca611.png ../../_images/6bb70494cf014bdc6cddb594326668c68dfe0a51b44517af7fb05a81f2b297bb.png

4.3.3. The showtype option#

In this example the difference with respect first 'keep scenario baseline values are once again shown. This time the showtype option has been set to growth. As a result the data is displayed as the difference in the growth rates between the simulation scenarios and the growth rate in the baseline scenario – remembering that keep_plot is calculating the differences with respect to the first kept scenario. Its name need not have been baseline, this was just a decision made in preparing the shocks that facilitated the kind of comparisons to be displayed.

mpak.keep_plot('PAKNYGDPMKTPCN PAKNEIMPGNFSKN', diff=1,showtype='growth', legend=True);
../../_images/c108aa7145f72e741ee6c08a41a7732db9cbf0ad748b97dcacfce5e17cccaba7.png ../../_images/75fbf506a7533baef2580814645b7075b88166229129d60e07ea599f26725ee3.png

4.3.4. The diffpct option#

Setting diffpct=True instructs .keep_plot() to display the data as a percent deviation from the first kept scenario, which in this case was the baseline scenario. \(\big(\frac{x_{shock}-x_{original}}{x_{shock}}\big) * 100\)

mpak.keep_plot('PAKNYGDPMKTPCN  PAKNEIMPGNFSKN', diffpct=True,showtype='level',legend="Change in level as a % of first keep scenario");
../../_images/5424721b538112e440a853964d2ca595e4fe2baff835c154377876b0ba3cd831.png ../../_images/d2270683c9ba6ba1b61444dcab279c26dd073e4b5047975313e13284a9b832bb.png

4.3.5. The .keep_switch() method#

The .keep_switch() method restricts the number of scenarios on which subsequent calls to .keep_plot() (and .keep_plot_multi()) are executed on. .keep_switch() can be passed a list of scenarios or using a wildcard selector.

4.3.5.1. The .keep_solutions.keys() method#

The .keep_solutions.keys() method displays a list of the solutions that have been kept previously.

mpak.keep_solutions.keys()
dict_keys(['Baseline', '$25 increase in oil prices 2025-27', '2.5% increase in C 2025-40', '2.5% increase in C 2025-27 -- exog whole period', '2.5% increase in C 2025-27 -- exog whole period --KG=True', '2.5% increase in C 2025-27 -- temporarily exogenized', '1% of GDP increase in FDI and private investment (AF shock)'])

To specify exactly which scenarios to show in a keep_plot, the scenarios= option of .keepswitch() must be initialized with a “|” delimited string of the names of the scenarios (retrieved above) that are to be displayed.

By placing the .keepswitch() in a with clause the scenario restriction will only apply to indented lines that follow the with construct.

As before the scenarios are compared with the first scenario in the keep dictionary, which in this instance happens to be named ‘baseline’.

with mpak.keepswitch(scenarios='2.5% increase in C 2025-40|2.5% increase in C 2025-27 -- exog whole period|2.5% increase in C 2025-27 -- exog whole period --KG=True|2.5% increase in C 2025-27 -- temporarily exogenized'):
    mpak.keep_plot('PAKNYGDPMKTPKN PAKGGBALOVRLCN PAKGGDEBTTOTLCN',start=2020,end=2040,diff=False,showtype='growth',legend=True);
../../_images/4d4082f1eb35f31728b16dfc3ce40cde3588dab0fdb1f28afb94397881f0437f.png ../../_images/79c4faa9eebd093cebdf32aa5f4137a7d51775132845aedc327b97c04275222a.png
4.3.5.1.1. The .keep_solutions.keys() method with diff=True#

If .keep_plot() is called on a sub-set of the kept solutions, the comparison will be made relative to the first scenario in the .keep_switch call.

Thus in the example below (which repeats the previous call, but sets diff=True, all of the shocks are calculated relative to the shock named ‘2.5% increase in C 2025-40’. Thus there are only three lines (not four as when diff=False).

with mpak.keepswitch(scenarios='2.5% increase in C 2025-40|2.5% increase in C 2025-27 -- exog whole period|2.5% increase in C 2025-27 -- exog whole period --KG=True|2.5% increase in C 2025-27 -- temporarily exogenized'):
    mpak.keep_plot('PAKNYGDPMKTPKN PAKGGBALOVRLCN PAKGGDEBTTOTLCN',start=2020,end=2040,diff=True,showtype='growth',legend=True);
../../_images/f01298e8430e2cc5a3496f23f112de4c3e4f6ceebf7c1d674200a266f8b8fdc9.png ../../_images/a8cbfed32c80ccd213688f5a6622b0eb66bd1bc5bce7fc4d20b883e5b14def9f.png

If, as before, the objective was to compare the four scenarios with the baseline scenario, the baseline scenario would have to be added to the .keep_switch() call as below:

with mpak.keepswitch(scenarios='baseline|2.5% increase in C 2025-40|2.5% increase in C 2025-27 -- exog whole period|2.5% increase in C 2025-27 -- exog whole period --KG=True|2.5% increase in C 2025-27 -- temporarily exogenized'):
    mpak.keep_plot('PAKNYGDPMKTPKN PAKGGBALOVRLCN PAKGGDEBTTOTLCN',start=2020,end=2040,diff=True,showtype='growth',legend=True);
../../_images/e80c5f90e37adff948ece687be7cc2124ba8b977119371304f88bc78664d78d8.png ../../_images/829e35b7f5bdae76f7b2183abd58edf729201c345e0babe9b471a8811a418a81.png

Warning

.keep_switch() and .keep_plot() with option diff=True When the .keep_plot() option diff is set to True, the figures will be generated as comparisons with the values of the first scenario specified in the .keep_switch() command. In most instances users will want to use the baseline scenario as this comparison point. Failure to do so when this was the intention/expectation of the user, may generate unexpected results.

4.3.5.2. Keepswitch with wildcard selection#

Below a series of plots are generated using a wildcard selector in the keepswitch clause.

with mpak.keepswitch(scenarios='*2025*'):
    mpak.keep_plot('PAKNYGDPMKTPKN PAKNECONPRVTKN',showtype='growth',legend=True,start=2020,end=2040);
../../_images/7d1aa1ca7b80bc840d25f12a2679f4c190d82eecf72cf6f12a4d247df9830ea2.png ../../_images/53ecd99caf80c691a952bb6131692e98a3a3fe68aac682da277baf00fe32b45e.png

4.4. The .keep_plot_multi() method#

The .keep_plot_multi() method allows several charts to be displayed in a grid. The size of each chart can be set with the size=(w,h) option, where the units of with and height are in centimetres. This call differs from .keep_plot() by displaying the difference of multiple variables from one scenario versus the initial scenario, versus the .keep_plot() which shows all scenarios for just one variable.

Note

.Keep_plot vs .keep_plot_multi()

Both .Keep_plot and .keep_plot_multi() display multiple graphs comparing results for one or more variables.

Method

nature of displayed lines

.keep_plot()

One graph per series displayed, multiple scenarios on each graph

.keep_plot_multi()

Many different variables displayed on each graph, each emanating from a different scenario

with mpak.set_smpl(2000,2040):
    with mpak.keepswitch(scenarios="baseline *exog*"):
        var_figs = mpak.keep_plot_multi('PAKNYGDPMKTPKN PAKNECONPRVTKN PAKNEGDIFTOTKN PAKNEIMPGNFSKN PAKNEEXPGNFSKN',2010,2040,keep_dim=0,legend=1
                                ,size=(20,20) ,diffpct=True,title='Scenario Comparison'  );
../../_images/110b073013bb48ae907f8e787aab3de274c42258c568c7e059e8bf4220299939.png ../../_images/d0207b592c68e6eba3c886c8bf3e4c0e913066366b28e2733a47ee824b534250.png ../../_images/cc8753508708321924a1cc407bbc1e235cbb4e25eb114ffe1f16af04929078bb.png

As indicated earlier both keep_plot() and keep_plot_multi() return a variable (a grid of mathplot figure objects) that can be used to embellish or modify the figures produced by the automatic routines.

For example the charts can be resized.

var_figs2=var_figs
var_figs2.set_size_inches(12,7)
var_figs2
../../_images/77892988a30aeecd81928c8eb3ed586733f77a0c65e4642e398ca3f913a6ca93.png

Individual charts can be deleted from the grid.

Note

The grid representation of the individual charts is returned as a 0-based vector of charts. Thus the first figure is the zeroeth and the second is the first.

4.4.1. Delete a chart from the grid#

A chart can be deleted from the grid by referencing it and calling the .remove() method.

var_figs.axes[1].remove()
var_figs
../../_images/39c67b62c87ca3843e3347b7c40982ea87f139397aeb2c4f85a480a83f820e8e.png

The same mechanism can be used to revise the titles of the indidividual charts and annotate them.

var_figs.axes[0].set_title('Impact of a 2.5% increase in C using exog method');    # many properties can be set afterward 
var_figs.axes[1].set_title('Impact of a 2.5% increase in C using temporary exog method');
var_figs
../../_images/d66d903897993164e011855f255a4b5f3c2008016206d7f520ab2213280fa53d.png
var_figs.axes[0].set_xlabel('Year')
var_figs.axes[0].set_ylabel('Change percent\nchange in level',fontsize=10)
var_figs.axes[0].yaxis.set_label_coords(-0.1,1.02)
var_figs.axes[0].xaxis.set_label_coords(.95,-.06)
var_figs
../../_images/a434d05a9337e9239a0c03450c8593d062585b08d2938bddf6cd9e82e9623a9c.png

Variables pointing to the individual charts can be defined and used to make modifications to individual charts within the overall figure.

#del fig1
#del fig2
fig1=var_figs.axes[0]
fig2=var_figs.axes[1]
fig2.set_ylabel('Percent change\nin level',fontsize=10)
fig2.yaxis.set_label_coords(-0.1,1.02) #place axes labels
fig2.xaxis.set_label_coords(.95,-.06)



fig2.text(2030.,1.4, 'Some text in a box', 
          color='yellow',bbox=dict(facecolor='red', alpha=0.5));
fig2.text(2035.,-1., 'Some nice text', 
          style='italic',color='green');
          
var_figs
          
../../_images/0fe79980d587e4bd57c3cdd6bd5a9c2e0a9e36be8144cafa5e9a4a9023026f2c.png